﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using OneSoft.EntityCore;
using OneSoft.DataAccess;
using BySoft.Server.Common.WebCommon;

using BySoft.Server.Consts;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Schema;
using BySoft.Server.DataAccess;
namespace BySoft.Server.Business
{
    public partial class SysModule
    {
        /// <summary>
        /// 获取系统功能模块列表
        /// </summary>
        /// <returns></returns>
        public static DataTable GetSysModuleList()
        {
            try
            {
                return SysModuleDA.GetSysModuleList();
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 获取某一用户有权限的顶级菜单
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static DataTable GetTopModuleListByUser(string userId)
        {
            try
            {
                return SysModuleDA.GetTopModuleListByUser(userId);
            }
            catch
            {
                throw;
            }
        }
        /// <summary>
        /// 获取某一用户有权限的顶级菜单
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static DataTable MyGetTopModuleListByUser(string userId)
        {
            try
            {
                return SysModuleDA.MyGetTopModuleListByUser(userId);
            }
            catch
            {
                throw;
            }
        }


        public static DataTable GetTopModuleListByAdmin()
        {
            try
            {
                return SysModuleDA.GetTopModuleListByAdmin();
            }
            catch
            {
                throw;
            }
        }


        public static DataTable GetHomeTopModuleList(string sParentModuleID)
        {
            try
            {
                return SysModuleDA.GetHomeTopModuleList(sParentModuleID);
            }
            catch
            {
                throw;
            }
        }

        public static DataTable GetHomeTopModuleList()
        {
            try
            {
                return SysModuleDA.GetHomeTopModuleList();
            }
            catch
            {
                throw;
            }
        }


        public static DataTable MyGetTopModuleListByAdmin()
        {
            try
            {
                return SysModuleDA.MyGetTopModuleListByAdmin();
            }
            catch
            {
                throw;
            }
        }
        /// <summary>
        /// 获取某一用户有权限的二级菜单
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static DataTable GetSedMenuListByUser(string userId)
        {
            try
            {
                return SysModuleDA.GetSedMenuListByUser(userId);
            }
            catch
            {
                throw;
            }
        }

        public static DataTable GetSedMenuListByAdmin()
        {
            try
            {
                return SysModuleDA.GetSedMenuListByAdmin();
            }
            catch
            {
                throw;
            }
        }

        public static DataTable MyGetSedMenuListByAdmin()
        {
            try
            {
                return SysModuleDA.MyGetSedMenuListByAdmin();
            }
            catch
            {
                throw;
            }
        }


        /// <summary>
        /// 根据地址查询顶级菜单id
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetTopModuleIdByUrl(string url)
        {
            if (SysModuleDA.GetTopModuleIdByUrl(url) != null)
            {
                return SysModuleDA.GetTopModuleIdByUrl(url).ToString();
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 根据URL地址得到二级模块信息
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static SysModuleEntity GetLeftModuleInfoByUrl(string url)
        {
            //SysModuleEntity enModoule = new SysModuleEntity();
            //DataTable dt = SysModuleDA.GetLeftModuleInfoByUrl(url);
            //if (dt != null && dt.Rows.Count > 0)
            //{
            //    enModoule.Moduleid = dt.Rows[0][SysModuleTable.C_MODULEID].ToString();
            //    enModoule.Modulename = dt.Rows[0][SysModuleTable.C_MODULENAME].ToString();
            //}
            //return enModoule;
            return SelectionCatche.GetModuleByUrl(url);
        }

        /// <summary>
        /// 得到二级模块List
        /// </summary>
        /// <returns></returns>
        public static DataTable GetLeftModuleList()
        {
            //return SysModuleDA.GetLeftModuleList();
            return SelectionCatche.GetSecondModuleList();

        }

        #region 缓存刷新
        /// <summary>
        ///  模块的缓存刷新
        /// </summary>
        public static void RefreshWebCaching()
        {

            DataSet ds = SysModule.GetSysModuleAll();
            WebCaching.ALLMODULESCaching = ds;
        }
        #endregion


        /// <summary>
        /// 判断模块是否存在子页面
        /// </summary>
        /// <param name="moduleID"></param>
        /// <returns></returns>
        public static bool IsExistChildModule(string moduleID)
        {
            return SysModuleDA.IsExistChildModule(moduleID);
        }


        public static DataTable GetSystemManageModuleByUserID(string UserID)
        {
            return SysModuleDA.GetSystemManageModuleByUserID(UserID);
        }

        public static string GetNextModuleID(string strParentID)
        {
            return SysModuleDA.GetNextModuleID(strParentID);
        }

        public static int GetNextOrderID(string strParentID)
        {
            return SysModuleDA.GetNextOrderID(strParentID);
        }

        //public static void Save(SysModuleEntity entity, List<SysModuleEntity> lst, SysContenttypeEntity entType)
        //{
        //    using (DataAccessBroker broker = DataAccessFactory.Instance())
        //    {
        //        try
        //        {
        //            broker.BeginTransaction();
        //            DataAccessCommon.Insert(entity, true, broker);

        //            foreach (SysModuleEntity item in lst)
        //            {
        //                DataAccessCommon.Insert(item, true, broker);
        //            }
        //            entType.ModulePrimaryID = entity.ModulePrimaryID;
        //            DataAccessCommon.Insert(entType, true, broker);

        //            SysModuleDA.UpdateUrl(broker);


                    
        //            broker.Commit();
        //        }
        //        catch
        //        {
        //            broker.Rollback();
        //            throw;
        //        }
        //    }
        //    UpdateWache();
        //}

        private static void UpdateWache()
        {
            RefreshTopMenuCaching("OP01");
            RefreshTopMenuCaching("CS01");
            RefreshOfficeMenuCaching("OP01");
        }

        public static void CreateDefaultMenu()
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    broker.ExecuteCommand("CreateDefaultMenu");
                    broker.Commit();
                }
                catch
                {
                    broker.Rollback();
                    throw;
                }
            }
        }

        public static DataTable Select(string strtxtModuleID, string strtxtModuleName, string strtxtParentModuleID, string strchkCanView)
        {
            return SysModuleDA.Select(strtxtModuleID, strtxtModuleName, strtxtParentModuleID, strchkCanView);
        }

        public static DataTable Select()
        {
            return SysModuleDA.Select();
        }
        public static bool IsExistsModuleID(string strModuleID)
        {
            return SysModuleDA.IsExistsModuleID(strModuleID);
        }

        public static DataTable SelectHomeData(string sType, string strCode, string sIndex)
        {
            return SysModuleDA.SelectHomeData(sType, strCode, sIndex);
        }

        public static DataTable SelectHomeContent(string strCode)
        {
            return SysModuleDA.SelectHomeContent(strCode);
        }

        public static DataTable SelectModuleAllContent(string strCode)
        {
            return SysModuleDA.SelectModuleAllContent(strCode);
        }

        public static DataTable SelectLeftMenu()
        {
            return SysModuleDA.SelectLeftMenu();
        }

        public static DataTable SelectListModuleName(string strCode)
        {
            return SysModuleDA.SelectListModuleName(strCode);
        }

        public static DataTable SelectRightList(string strCode ,string sType)
        {
            return SysModuleDA.SelectRightList(strCode, sType);
        }

        public static DataTable SelectContent(string strCode, string sType)
        {
            return SysModuleDA.SelectContent(strCode, sType);
        }

        public static DataTable SelectCultureModule(string strCode)
        {
            return SysModuleDA.SelectCultureModule(strCode);
        }


        public static void UpdateViewNum(string strCode,int iViewNum, string sPageType)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    SysModuleDA.UpdateViewNum(strCode, iViewNum,sPageType, broker);//更新删除位
                    broker.Commit();
                }
                catch
                {
                    broker.Rollback();
                    throw;
                }
            }
        }

        public static bool IsExistsChild(string strCode)
        {
            return SysModuleDA.IsExistsChild(strCode);
        }

        public static void UpdateStatus(string strCode)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    SysModuleDA.UpdateStatus(strCode, broker);//更新删除位
                    broker.Commit();
                }
                catch
                {
                    broker.Rollback();
                    throw;
                }
            }
            UpdateWache();
        }

        public static string SelectPreSameParent(string strParent,string strOrderNo)
        {
            return SysModuleDA.SelectPreSameParent(strParent, strOrderNo);
        }

        public static string SelectNextSameParent(string strParent, string strOrderNo)
        {
            return SysModuleDA.SelectNextSameParent(strParent, strOrderNo);
        }

        public static void SetOrderNo(string strCode,string strOtherCode,string strOrderNo)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    SysModuleDA.SetOrderNo(strCode, strOtherCode, strOrderNo, broker);//更新删除位
                    broker.Commit();
                }
                catch
                {
                    broker.Rollback();
                    throw;
                }
            }
        }

        public static string SelectMaxModuleID(string strParent)
        {
            return SysModuleDA.SelectMaxModuleID(strParent);
        }

        public static int SelectOrderNo(string strParent,string strPageType,string strCanView)
        {
            int a = SysModuleDA.SelectOrderNo(strParent, strPageType, strCanView);
            //if (a == 0)
            //{
            //    a = SysModuleDA.SelectParentOrderNo(strParent);
            //}
            return a;
        }

        public static DataTable SelectHead(string strCode)
        {
            return SysModuleDA.SelectHead(strCode);
        }

        public static void Edit(SysModuleEntity entity)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    DataAccessCommon.UpdateOnly(entity, broker);
                    SysModuleDA.UpdateUrl(broker);

                    broker.Commit();
                }
                catch
                {
                    broker.Rollback();
                    throw;
                }
            }
            UpdateWache();
                    
        }

        public static string CheckUsed(string strCode)
        {
            string str = string.Empty;
            DataTable dt = SysModuleDA.CheckUsed(strCode);
            foreach (DataRow row in dt.Rows)
            {
                str += row["tablename"].ToString() + ",";
            }
            return str;
        }

        public static DataTable SelectSearch()
        {
            return SysModuleDA.SelectSearch();
        }

        public static DataTable SearchData(string sKeyWords, string sModulePrimaryID, string sPageType)
        {
            return SysModuleDA.SearchData(sKeyWords, sModulePrimaryID, sPageType);
        }

        public static void RefreshTopMenuCaching(string sParentModuleID)
        {

            DataTable dt =  SysModuleDA.GetHomeTopModuleList(sParentModuleID);;
            WebCaching.ALLTOPMENUCaching = dt;//缓存头部菜单更新
        }

        public static void RefreshOfficeMenuCaching(string sParentModuleID)
        {

            DataTable dt = SysModuleDA.GetHomeTopModuleList(sParentModuleID); ;
            WebCaching.ALLOFFICEMENUCaching = dt;//缓存科室专栏更新
        }

        public static DataTable SelectCheck(bool IsAll)
        {
            return SysModuleDA.SelectCheck(IsAll);
        }

        public static DataTable SelectView(bool IsAll)
        {
            return SysModuleDA.SelectView(IsAll);
        }
    }
}
